<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>别踩白块</title>
  <style>
    body {
      padding: 0px;
      margin: 0px;
    }

    .box {
      width: 500px;
      height: 800px;
      background-color: rgba(40, 144, 30, 0.37);
      margin: 0 auto;
      margin-top: 2%;
      position: relative;
    }

    .fonttitle {
      margin-left: 25%;
      margin-top: 10%;
      font-size: 64px;
      color: black;
      float: left;
      text-shadow: rgb(243, 71, 108) -4px -6px 3px, rgb(113, 238, 238) 8px 1px 0px;
    }

    #bg {
      border-collapse: collapse;
      margin-left: 1.6%;
      margin-top: 35%;
      position: absolute;
    }

    #bg tr {
      width: 475px;
      height: 150px;
      /* background-color: black; */
    }

    #bg tr td {
      width: 158px;
      height: 150px;
      background-color: white;
      border: 1px solid black;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="fonttitle">别踩白块</div>
    <table border="1" id="bg">
      <tr id="tr">
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </table>
  </div>
  <script>
    var body = document.body;
    var tb = document.getElementById('bg');
    var rows = bg.rows;
    // console.log(rows[0]);
    for (var i = 0; i < rows.length; i++)//循环遍历所有的tr行
    {
      //取得第几行下面的td个数，再次循环遍历该行下面的td元素
      var sj = Math.ceil(Math.random() * 3) - 1;
      //获取某行下面的某个td元素
      var cell = rows[i].cells[sj];
      cell.style.backgroundColor = 'rgb(176, 176, 245)';
    }
    function delectbg() {
      tb.deleteRow(3);
    }
    function addtr() {
      var newTr = document.createElement('tr');
      var newTd1 = document.createElement('td');
      var newTd2 = document.createElement('td');
      var newTd3 = document.createElement('td');

      newTr.appendChild(newTd1);
      newTr.appendChild(newTd2);
      newTr.appendChild(newTd3);

      // tb.insertRow(newTr);
      tb.firstElementChild.insertBefore(newTr, tb.firstElementChild.firstElementChild);

      var sj = Math.ceil(Math.random() * 3) - 1;
      //获取某行下面的某个td元素
      var cell = rows[0].cells[sj];
      cell.style.backgroundColor = 'rgb(176, 176, 245)';
    }
    function qiaoji() {
      delectbg();
      addtr();
    }
    body.addEventListener('keydown', function () {
      // console.log(event);
      if (event.key == 'ArrowDown' && rows[3].cells[1].style.backgroundColor == 'rgb(176, 176, 245)') {
        qiaoji();
      } else if (event.key == 'ArrowLeft' && rows[3].cells[0].style.backgroundColor == 'rgb(176, 176, 245)') {
        qiaoji();
      } else if (event.key == 'ArrowRight' && rows[3].cells[2].style.backgroundColor == 'rgb(176, 176, 245)') {
        qiaoji();
      } else {
        alert("按错了！");
      }
    });
  </script>
</body>
</html>